package com.TPNexus.dao;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

import org.hibernate.Query;
import org.hibernate.Session;
import com.TPNexus.model.CatalogItem;
import com.TPNexus.model.Product;

@Named
@ApplicationScoped
public class CatalogItemDAO {
	private Session session=HibernateInitial.getSession();
	public ArrayList<CatalogItem> getCatalogItemList() {
		session.beginTransaction();
		ArrayList<CatalogItem> list = new ArrayList<CatalogItem>();
		Query query = session
				.createQuery("from CatalogItem c where c.status=true");
		list = (ArrayList<CatalogItem>) query.list();

		session.getTransaction().commit();
		return list;
	}

	public int addNewCatalogItem(CatalogItem catalogItem) {
		int result;
		try {
			session.beginTransaction();
			session.save(catalogItem);
			session.getTransaction().commit();
			result = 1;
		} catch (RuntimeException e) {
			session.getTransaction().rollback();
			System.out.println(e.toString());
			result = 0;
			throw e;
		}
		return result;
	}

	public int deleteCatalogItem(CatalogItem catalogItem) {
		int result;
		try {
			session.beginTransaction();
			catalogItem.setStatus(false);
			session.update(catalogItem);
			session.getTransaction().commit();
			result = 1;
		} catch (RuntimeException e) {
			session.getTransaction().rollback();
			System.out.println(e.toString());
			result = 0;
			throw e;
		}
		return result;

	}

	public CatalogItem getCatalogItemDetail(long id) {
		CatalogItem result;
		try {
			session.beginTransaction();
			result = (CatalogItem) session.get(CatalogItem.class, id);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			session.getTransaction().rollback();
			System.out.println(e.toString());
			throw e;
		}
		return result;
	}

	public int updateCatalogItem(CatalogItem catalogItem) {

		int result;
		try {
			session.beginTransaction();
			session.update(catalogItem);
			session.getTransaction().commit();
			result = 1;
		} catch (RuntimeException e) {
			session.getTransaction().rollback();
			System.out.println(e.toString());
			result = 0;
			throw e;
		}
		return result;

	}
}
